<template>
  <!-- <BaseMainSlot>
    <template v-slot:searchbox>
      <el-collapse v-model="activeName" accordion @change="handleChange">
        <el-collapse-item name="1">
          <template slot="title">
            <div class="select-btns search-btns" @click.stop="changeSelect()">
              <i class="el-icon-circle-check"></i>
              <p v-if="activeName === '1'">收起筛选</p>
              <p v-if="activeName === ''">展开筛选</p>
            </div>
          </template>
          <div class="search-btn search-btns" @click.stop="queryPageList()">
            <i class="el-icon-circle-check"></i>
            <p>查询</p>
          </div>
          <div class="select-box-content">
            <div class="select-box-content-name">部门名称</div>
            <el-input v-model="departmentName" clearable @change="queryPageList" />
          </div>
        </el-collapse-item>
      </el-collapse>
    </template>
    <template v-slot:btnsBar>
      <ActionBar :add-active="addActive" :del-active="delActive" :page-no="159" @add="add" @del="del" />
      <ChooseHomeBase :categoryList="categoryList" @searchBar="searchBar"></ChooseHomeBase>
    </template>
    <template v-slot:content>
      <el-table ref="tableData" v-loading="loading" highlight-current-row :data="tableData" stripe border
        height="calc(100% - 50px)" :row-key="row => { return row.departmentId; }"
        @selection-change="handleSelectionChange" @select="select" @select-all="selectAll">
        <el-table-column type="selection" width="55" :reserve-selection="true"
          :selectable="(row) => { return !row.selectable }" />
        <el-table-column type="index" label="序号" width="50">
          <template slot-scope="scoped">{{ scoped.row.departmentId ? scoped.$index + 1 : '' }}</template>
        </el-table-column>
        <el-table-column prop="departmentName" label="部门名称" />
        <el-table-column label="操作" width="200">
          <template v-if="scoped.row.departmentId" slot-scope="scoped">
            <div class="table-btns-box">
              
              <div class="table-btns edit-btns" @click="edit(scoped.row)">
                <p>修改</p>
              </div>
             
              <div class="table-btns del-btns" @click="delOne(scoped.row)">
                <p>删除</p>
              </div>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <Pagination :page-size="pageSize" :current-page="currentPage" :total-size="totalSize"
        @changePageSize="changePageSize" @changeCurrentPage="changeCurrentPage" />
      <AddEditDialog ref="addEditDialog" :now-chance-stau="nowChanceStau" :title-text="titleText"
        :edit-object="editObject" :show.sync="showAddEditDialog" @closeDialog="closeDialog" @cancelDialog="cancel" />
    </template>
  </BaseMainSlot> -->


  <MainSlot>
    <template v-slot:searchForm>
      <SearchForm v-if="formItemList.length" :formItemList="formItemList" @search="search">
      </SearchForm>
    </template>
    <template v-slot:btn>
      <BtnSlot :btnList="btnList" @handleEvent="getBtnEvent"></BtnSlot>
    </template>
    <template v-slot:table>
      <ChooseHomeBase :categoryList="categoryList" @searchBar="searchBar"></ChooseHomeBase>
      <el-table ref="tableData" v-loading="loading" highlight-current-row :data="tableData" stripe border
        height="calc(100% - 70px)" :row-key="row => { return row.departmentId; }"
        @selection-change="handleSelectionChange" @select="select" @select-all="selectAll">
        <el-table-column type="selection" width="55" :reserve-selection="true"
          :selectable="(row) => { return !row.selectable }" />
        <el-table-column type="index" label="序号" width="50">
          <template slot-scope="scoped">{{ scoped.row.departmentId ? scoped.$index + 1 : '' }}</template>
        </el-table-column>
        <el-table-column prop="departmentName" label="部门名称" />
        <el-table-column label="操作" width="200">
          <template v-if="scoped.row.homeBaseId" slot-scope="scoped">
            <span class="table-btns edit-btns successText" @click="edit(scoped.row)">修改</span>
            <span class="table-btns del-btns errorText" @click="delOne(scoped.row)">删除</span>
          </template>
        </el-table-column>
      </el-table>
    </template>
    <template v-slot:pagination>
      <Pagination @current-change="changeCurrentPage" :page="currentPage" :size="pageSize" :total="totalSize">
      </Pagination>
    </template>
    <template v-slot:dialogModule>
      <AddEditDialog ref="addEditDialog" :now-chance-stau="nowChanceStau" :title-text="titleText"
        :edit-object="editObject" :show.sync="showAddEditDialog" @closeDialog="closeDialog" @cancelDialog="cancel" />
    </template>
  </MainSlot>
</template>

<script>
import MainSlot from "@/components-production-management/MainSlot";
import Pagination from '@/components-production-management/Pagination';
import SearchForm from '@/components-production-management/SearchForm';
import BtnSlot from '@/components-production-management/BtnSlot';

import AddEditDialog from "./components/add-edit-dialog.vue";
import { mymixin } from "@/utils/mixin";
import { mapGetters } from "vuex";
import ChooseHomeBase from "@/components/ChooseHomeBase/index.vue"

import DepartmentApi from "@/api/basic-info-manage/department";
export default {
  name: "DepartmentManage",
  components: { BtnSlot, Pagination, SearchForm, MainSlot, AddEditDialog ,ChooseHomeBase},
  mixins: [mymixin],
  data() {
    return {
      // 所搜条件：部门名称
      btnList: ['add', 'delete'],
      departmentName: "",
      checkedIdList: [],
      categoryList: [],
      formItemList: [
        {
          label: '部门名称',
          type: 'input',
          param: 'departmentName',
          defaultSelect: '', // 下拉框默认选中项
          multiple: false,
          isShow: true
        },
      ],
    };
  },
  computed: {
    ...mapGetters({
      judgePermission: "permission/judgePermission"
    })
  },

  watch: {},
  created() {
    this.queryPageList();
    this.categoryList = JSON.parse(sessionStorage.getItem('homeBaseList'))
  },
  mounted() { },
  methods: {
    searchBar() {
      this.queryPageList();
    },
    search(data) {
      this.departmentName = data.departmentName;
      this.queryPageList();
    },
    queryPageList() {
      let params = {
        page: this.currentPage,
        size: this.pageSize,
        entity: {
          departmentName: this.departmentName
        }
      };
      DepartmentApi.queryPageByCondition(params).then(res => {
        if (res.success === true) {
          let data = res.data || {};
          this.totalSize = data.totalElements;
          // 赋值数组
          this.tableData = data.content;
        }
      });
    },
    changePageSize(pageSize) {
      this.pageSize = pageSize;
      this.queryPageList();
    },
    changeCurrentPage(currentPage) {
      this.currentPage = currentPage;
      this.queryPageList();
    },
    selectAll(list) {
      let notvalue = true;
      list.forEach(item => {
        if (!item.departmentId) {
          notvalue = false;
        }
      });
      if (notvalue === false) {
        this.$message.warning("不可选择空数据！");
      }
    },
    select(list) {
      let notvalue = true;
      list.forEach(item => {
        if (!item.departmentId) {
          notvalue = false;
        }
      });
      if (notvalue === false) {
        this.$message.warning("不可选择空数据！");
      }
    },
    handleSelectionChange(list) {
      this.checkedIdList = [];
      let notvalue = true;
      let emptyList = [];
      this.checkedIdList = list.map(item => {
        if (item.departmentId) {
          return item.departmentId;
        } else {
          notvalue = false;
          emptyList.push(item);
          return "";
        }
      });
      if (notvalue === false) {
        // this.$message.warning("不可选择空数据！");
        this.toggleNoSelection(emptyList);
        return;
      }
      if (this.checkedIdList.length >= 1) this.delActive = true;
      else this.delActive = false;
    },

    //获取选中Btn
    getBtnEvent(flag) {
      if (flag == 'add') {
        this.add();
      } else if (flag == 'delete') {
        this.delmessage();
      }
    },
    // 单个删除
    delOne(data) {
      this.$confirm("此操作将永久删除该项, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          DepartmentApi.delById({ id: data.departmentId }).then(res => {
            if (res.success) {
              this.$message.success("删除成功");
              this.queryPageList();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    // 表格删除
    delmessage() {
      this.$confirm("此操作将永久删除该项, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",

      })
        .then(() => {
          DepartmentApi.delListById({ idList: this.checkedIdList }).then(res => {
            if (res.success) {
              this.$message.success("删除成功");
              this.queryPageList();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });

    },
    // 添加
    add() {
      this.nowChanceStau = "add";
      this.titleText = "添加";
      this.addActive = false;
      this.delActive = false;
      this.showAddEditDialog = true;
    },
    // 修改
    edit(row) {
      if (row.departmentId) {
        this.editObject = JSON.parse(JSON.stringify(row));
        this.nowChanceStau = "edit";
        this.titleText = "修改";
        this.addActive = false;
        this.delActive = false;
        this.showAddEditDialog = true;
      } else {
        this.editObject = {};
      }
    }
  }
};
</script>

<style lang="scss" scoped>
.table-btns {
  padding: 0 10px;
  cursor: pointer;
}

.imageStyle {
  height: 100px;
  width: 200px;
  border-radius: 4px;
  margin: 0 auto;
  background-color: #cecece3f;
  display: flex;
  align-items: center;
  justify-content: space-around;
  font-size: 16px;
  color: #999999;
}
</style>
